import { createStore } from 'vuex'

const moduleA = {
  state: () => {
    return {
       username: 'moduleA'
    }
  }, 
  getters:{
    newName(state){
      return state.username
    }
  },
  mutations: {
    updateName(state){
      state.username += state.username
    }
  }
}

const moduleB = {
  namespaced: true,
  state: () => {
    return {
      username: 'moduleB'
    }
  },
  getters:{
    newName(state){
      return state.username
    }
  },
  mutations: {
    updateName(state){
      state.username += state.username
    }
  },
  actions: {
    setName(ctx) {
      console.log(ctx);
      ctx.commit('updateName')
    }
  }
}


export default createStore({
  state: {
    username: 'zhangsan',
    age: 18
  },
  mutations: {
    setName (state, payload) {
      state.username = payload
    },
    setAge (state, payload) {
      state.age = payload
    }
  },
  actions: {
    updateAge (ctx, payload) {
      setTimeout(() => {
        ctx.state.age = payload
      }, 1000)
    }
  },
  modules: {
    moduleA,
    moduleB
  },
  getters: {

  }
})
